package civicinity.ist.psu.edu.profile;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import civicinity.ist.psu.edu.R;

public class TogglePreferences extends Preference{
	
	private boolean mIsToggleOn;
	private Context mContext;
	private SharedPreferences mPref;
	
	public TogglePreferences(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		mContext = context;
		// TODO Auto-generated constructor stub
	}
	
	public TogglePreferences(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		setLayoutResource(R.layout.preference_toggle_icon);
	}
	
	@Override
	public void onBindView(View view) {
		super.onBindView(view);
		
		mPref = PreferenceManager.getDefaultSharedPreferences(mContext);
		
		final ToggleButton tButton = (ToggleButton) view.findViewById(R.id.toggle_button);
		
		if(mIsToggleOn) {
			tButton.setChecked(true);
		}
		else {
			tButton.setChecked(false);
		}
		
		tButton.setOnClickListener(new OnClickListener() {
		    public void onClick(View v) {
		        // Perform action on clicks
		        if (tButton.isChecked()) {
		            Toast.makeText(mContext, "Notification On", Toast.LENGTH_SHORT).show();
		            
		            SharedPreferences.Editor editor = mPref.edit();
					editor.putBoolean("notification_on", true);
		    		editor.commit();
		    		
		            
		        } else {
		            Toast.makeText(mContext, "Notification Off", Toast.LENGTH_SHORT).show();
		            
		            SharedPreferences.Editor editor = mPref.edit();
					editor.putBoolean("notification_on", false);
		    		editor.commit();
		        }
		    }
		});
	}
	
	public void setToggleButtonChecked(boolean isToggleOn) {
		
		mIsToggleOn = isToggleOn;
		
		notifyChanged();
	}
}
